import 'package:flutter/material.dart';
import 'package:yuan_hang_app/utils/router_helper.dart';
import 'package:yuan_hang_app/utils/size_utils.dart';
import 'package:yuan_hang_app/utils/common_req_utils.dart';
import 'package:yuan_hang_app/views/home/view/book/book_detail_page.dart';
import 'package:yuan_hang_app/views/home/view/leads/leads_follow_up_info_page.dart';
import 'package:yuan_hang_app/widget/w_line.dart';
import 'package:yuan_hang_app/widget/w_text.dart';

import '../../../../../base/base_page.dart';
import '../../../../../res/w_colors.dart';
import '../../../../../res/w_dimens.dart';
import '../../../../../widget/status_btn.dart';
import '../../../model/leads_history.dart';
import '../../drive/drive_registration_detail_page.dart';

class LeadsHistoryList extends StatelessWidget {
  final List<LeadsHistory>? historyList;
  final Status? status;

  const LeadsHistoryList({super.key, this.historyList, this.status = Status.NONE});

  @override
  Widget build(BuildContext context) {
    return BasePage(
        status: status,
        child: ListView.builder(
            padding: EdgeInsets.zero,
            shrinkWrap: true,
            itemBuilder: _listItem,
            itemCount: historyList?.length,
        )
    );
  }

  Widget _listItem(context, index) {
    LeadsHistory item = historyList![index];
    return GestureDetector(
      onTap: () {
        if (item.clueFollowId != null) {
          RouteHelper.pushWidget(context, LeadsFollowUpInfoPage(followId: item.clueFollowId!));
        } else if (item.testDriverId != null) {
          RouteHelper.pushWidget(context, DriveRegistrationDetailPage(testDriveId: int.parse(item.testDriverId!),));
        } else if (item.appointmentId != null) {
          RouteHelper.pushWidget(context, BookDetailPage(bookId: int.parse(item.appointmentId!), cancelPerson: "", saleMan: ""));
        }
      },
      child: Container(
        color: WColors.white,
        padding: EdgeInsets.symmetric(vertical: 5.px, horizontal: 10.px),
        child: Row(
          mainAxisAlignment: MainAxisAlignment.center,
          crossAxisAlignment: CrossAxisAlignment.start,
          children: [
            Expanded(
              child: Column(
                crossAxisAlignment: CrossAxisAlignment.end,
                children: [
                  Text(
                    item.contactTime ?? "",
                    textAlign: TextAlign.right,
                    style: TextStyle(
                        fontWeight: FontWeight.bold,
                        fontSize: WDimens.tsLabel,
                        color: WColors.mainTextColor),
                  ),
                  SizedBox(
                    height: 10.px,
                  ),
                  WText(
                    item.contactPersonName ?? "",
                    style: TextStyle(
                        fontSize: WDimens.tsLabel, color: WColors.mainTextColor),
                  ),
                  SizedBox(
                    height: 10.px,
                  ),
                  WText(
                    item.contactType ?? "",
                    style: TextStyle(
                        fontSize: WDimens.tsLabel, color: WColors.mainTextColor),
                  ),
                ],
              ),
            ),
            SizedBox(
              width: 20.px,
            ),
            Column(
              children: [
                StatusBtn(
                  isBg: true,
                  isItemBtn: true,
                  text: CommonReqUtils.exchangeCodeToName(CommonReqUtils.intentRange, item.contactLevel),
                  color: CommonReqUtils.exchangeCodeToName(CommonReqUtils.intentRange, item.contactLevel) != "H"
                      ? WColors.hintTextColor
                      : WColors.primaryColor,
                  width: 28.px,
                  height: 28.px,
                  fontSize: 18.px,
                ),
                WLine(
                  height: 100.px,
                  width: 1.px,
                  marginTop: 10.px,
                )
              ],
            ),
            SizedBox(
              width: 20.px,
            ),
            Expanded(
                child: Column(
              crossAxisAlignment: CrossAxisAlignment.start,
              children: [
                Row(
                  children: [
                    WText(
                      CommonReqUtils.exchangeCodeToName(CommonReqUtils.leadsStatus, item.operatorAfterStatus),
                      style: TextStyle(
                          fontSize: WDimens.tsLabel,
                          // fontWeight: FontWeight.bold,
                          color: WColors.mainTextColor),
                    ),
                    if (item.clueFollowId != null || item.testDriverId != null || item.appointmentId != null)
                      Image.asset(
                        'images/ic_right.png',
                        width: 12.px,
                        height: 12.px,
                      )
                  ],
                ),
                SizedBox(
                  height: 10.px,
                ),
                WText(
                  item.description ?? "",
                  style:
                      TextStyle(fontSize: WDimens.tsLabel, color: WColors.hintTextColor),
                ),
                SizedBox(
                  height: 10.px,
                ),
                WText(
                  item.followMethod ?? "",
                  style:
                      TextStyle(fontSize: WDimens.tsLabel, color: WColors.hintTextColor),
                ),
              ],
            ))
          ],
        ),
      ),
    );
  }
}
